package com.yabushan.web.utils;


/**
 * 处理分页
 * @uthor:JJChen
 */
public class QueryForPage {
	private int page;	//当前的页码
	private int pageSize = 10;	//设置每页显示多少条记录
	private String startRownum;	//用于分页的开始数
	private String endRownum;	//用于分页的结束数
	private int totalPage;	//用于分页的总页数
	private int total;	//用于分页的总记录条数
	private String sortField; //用于排序的列
	private String sortOrder; //用于排序的方式
	private boolean judgePage; //用于判断是否分页
	
	public QueryForPage(){}
	public QueryForPage(int count, int page, int pageSize){
		this.page = page;
		this.pageSize = pageSize;
		doPage(count);
	}
	
	/**
	 * 初始化分页方法
	 * @author JJChen
	 * @date 2018年5月28日上午10:45:22
	 */
	public QueryForPage(int count, QueryForPage queryForPage){
		if(queryForPage != null){
			this.page = queryForPage.getPage();
			this.pageSize = queryForPage.getPageSize();
			this.judgePage = queryForPage.isJudgePage();
			//排序字段
			this.sortField = queryForPage.getSortField();
			this.sortOrder = queryForPage.getSortOrder();
			doPage(count);
		}else{
			startRownum = "";
			endRownum = "";
		}
	}
	
	/**
	 * 用于分页的方法
	 * @param count 查询数据库后获得的返回结果
	 */
	public void doPage(int count) {
		total = count;		//总记录数=参数count
		totalPage = total / pageSize;	//计算出一共有多少页
		int remainder = total % pageSize;	//记录总记录数除以页面大小，是否有余数
		if (total % pageSize != 0) {	//如果有余数
			totalPage += 1;		//总页数+1
		}
		
		if (page <= 0) {	//如果当前页数小于或者等于0
			page = 1;		//当前页码是首页
		}
		if (page >= totalPage) {								//如果页面大于等于总页
			page = totalPage;									//当前页是末页
			if (remainder != 0) {								//如果没有余数
				startRownum = String.valueOf(total - remainder);	//起始数=总记录数-余数
				endRownum = String.valueOf(total);				//结束数=最大记录数
				return;
			}				
		}
		startRownum = String.valueOf((page - 1) * pageSize);	//起始数=(页码-1)*页面大小
		endRownum = String.valueOf(page * pageSize);			//结束数=页码*页面大小
	}
	
	/**
	 * 重载用于分页的方法，添加设置页面大小的参数
	 * @param count 查询数据库后获得的返回结果
	 * @param pageSize 设置页面大小
	 */
	public void doPage(int count, int pageSize) {
		this.pageSize = pageSize;
		doPage(count);
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public String getStartRownum() {
		return startRownum;
	}
	public void setStartRownum(String startRownum) {
		this.startRownum = startRownum;
	}
	public String getEndRownum() {
		return endRownum;
	}
	public void setEndRownum(String endRownum) {
		this.endRownum = endRownum;
	}
	public int getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}

	public String getSortField() {
		return sortField;
	}

	public void setSortField(String sortField) {
		this.sortField = sortField;
	}

	public String getSortOrder() {
		return sortOrder;
	}

	public void setSortOrder(String sortOrder) {
		this.sortOrder = sortOrder;
	}
	public boolean isJudgePage() {
		return judgePage;
	}
	public void setJudgePage(boolean judgePage) {
		this.judgePage = judgePage;
	}
	
	
}
